package com.android.danaobank.mvp.fragment;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.viewpager.widget.ViewPager;

import com.android.danaobank.R;
import com.android.danaobank.adapter.MallFragmentPagerAdapter;
import com.android.pdd.baseapp.base.BaseFragment;
import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lg.meng.MessageEvent;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

import static com.android.danaobank.utils.Constants.MessageCode.MALL_INDEX_LAYOUT_HIDE;
import static com.android.danaobank.utils.Constants.MessageCode.MALL_INDEX_LAYOUT_SHOW;

public class TabMallFragment extends BaseFragment {
    @BindView(R.id.shop_index_tab_layout)
    ViewGroup shopLayout;
    @BindView(R.id.tab_layout)
    MagicIndicator magicIndicator;
    @BindView(R.id.view_pager)
    ViewPager viewPager;

    MallFragmentPagerAdapter mallFragmentPagerAdapter;

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_tab_mall;
    }

    @Override
    protected void initViews() {
        mallFragmentPagerAdapter = new MallFragmentPagerAdapter(getChildFragmentManager());
        viewPager.setAdapter(mallFragmentPagerAdapter);


        CommonNavigator commonNavigator = new CommonNavigator(activity);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return mallFragmentPagerAdapter.getCount();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context);
                colorTransitionPagerTitleView.setNormalColor(Color.GRAY);
                colorTransitionPagerTitleView.setSelectedColor(Color.BLACK);
                colorTransitionPagerTitleView.getPaint().setFakeBoldText(true);
                colorTransitionPagerTitleView.setText(mallFragmentPagerAdapter.getPageTitle(index));
                colorTransitionPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return colorTransitionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineWidth(ConvertUtils.dp2px(20));
                indicator.setRoundRadius(5);
                indicator.setColors(0xffF44336);
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);

        ViewPagerHelper.bind(magicIndicator, viewPager);
    }

    @Override
    protected void initData() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvent messageEvent) {
        switch (messageEvent.getCode()) {
            case MALL_INDEX_LAYOUT_HIDE:
                shopLayout.setVisibility(View.GONE);
                break;
            case MALL_INDEX_LAYOUT_SHOW:
//                shopLayout.setVisibility(View.VISIBLE);
                FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) shopLayout.getLayoutParams();
                layoutParams.topMargin =  - (Integer) messageEvent.getObj();
                shopLayout.setLayoutParams(layoutParams);
                break;
        }
    }


    @OnClick({R.id.category_expand_tv})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.category_expand_tv:
                ToastUtils.showShort("点击了：分类");
                break;
        }
    }
}
